package com.ny20231079080.dao.Impl;

import com.ny20231079080.dao.StudentDao;
import com.ny20231079080.until.DbUntil;
import com.ny20231079080.entity.Student;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class StudentDaolmpl implements StudentDao {
    private DbUntil  dbUntil = new DbUntil();

    @Override
    public Student login(String name, String password) throws Exception {
        Connection con = dbUntil.getCon();
        String sql = "select * from students where name = ? and password = ?";
        PreparedStatement preparedStatement = con.prepareStatement(sql);
        preparedStatement.setString(1, name);
        preparedStatement.setString(2, password);
        ResultSet  resultSet = preparedStatement.executeQuery();
        Student student = new Student();
        if (resultSet.next()){
            int id  = resultSet.getInt("student_id");
            String sname = resultSet.getString("name");
            String phone = resultSet.getString("phone");
            String spassword = resultSet.getString("password");
            student.setStusntid(id);
            student.setName(sname);
            student.setPhone(phone);
            student.setPassword(spassword);
            return student;
        }
        return null;
    }
    @Override
    public int insertStudent(Student student) throws SQLException, ClassNotFoundException {
        Connection con = dbUntil.getCon();
        String sql = "insert into students(name,phone,password) values(?,?,?)";
        PreparedStatement preparedStatement = con.prepareStatement(sql);
        preparedStatement.setString(1, student.getName());
        preparedStatement.setString(2, student.getPhone());
        preparedStatement.setString(3, student.getPassword());
        int rows = preparedStatement.executeUpdate();
        return rows;
    }

    @Override
    public Student findById(int studentId) throws SQLException, ClassNotFoundException {
        Connection con = dbUntil.getCon();
        String sql = "SELECT * FROM students WHERE student_id = ?";
        PreparedStatement ps = con.prepareStatement(sql);
        ps.setInt(1, studentId);
        ResultSet rs = ps.executeQuery();
        if (rs.next()) {
            Student student = new Student();
            student.setStusntid(rs.getInt("student_id"));
            student.setName(rs.getString("name"));
            student.setPhone(rs.getString("phone"));
            student.setPassword(rs.getString("password"));
            return student;
        }
        return null;
    }

    @Override
    public int update(Student student) throws SQLException, ClassNotFoundException {
        Connection con = dbUntil.getCon();
        String sql = "UPDATE students SET name=?, phone=?, password=? WHERE student_id=?";
        PreparedStatement ps = con.prepareStatement(sql);
        ps.setString(1, student.getName());
        ps.setString(2, student.getPhone());
        ps.setString(3, student.getPassword());
        ps.setInt(4, student.getStusntid());
        return ps.executeUpdate();
    }
}
